package com.zrrd.blog.question.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zrrd.blog.api.entities.Question;
import com.zrrd.blog.api.entities.Replay;
import com.zrrd.blog.question.mapper.QuestionMapper;
import com.zrrd.blog.question.mapper.ReplayMapper;
import com.zrrd.blog.question.service.IReplayService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zrrd.blog.util.base.Result;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 回答信息表 服务实现类
 * </p>
 *
 * @author chen
 * @since 2023-08-10
 */
@Service
public class ReplayServiceImpl extends ServiceImpl<ReplayMapper, Replay> implements IReplayService {

    @Autowired
    private QuestionServiceImpl questionService;

    @Autowired
    private QuestionMapper mapper;

    @Override
    public Result findByQuestionId(String questionId) {
        if(StringUtils.isEmpty(questionId)){
            return Result.error("问题id不能为空");
        }
        return Result.ok(baseMapper.findByQuestionId(questionId));
    }
    @Transactional
    @Override
    public Result deleteById(String id) {
        if(StringUtils.isEmpty(id)){
            return Result.error("问题id不能为空");
        }
        //收集要删除的评论id
        List<String> ids=new ArrayList<>();
        //将要删除的评论id先放入集合中
        ids.add(id);
        //批量查找该id下的子评论
        this.getIds(ids,id);
        //获取该id所属的实体
        Replay replay=baseMapper.selectById(id);
        int size=baseMapper.deleteBatchIds(ids);
        //删除完评论列表之后，要删除question中的回复数
        if(size>0){
            //根据实体里面的questionId找到对应的question
            Question question=mapper.selectById(replay.getQuestionId());
            //修改评论数
            question.setReply(question.getReply()-size);
            //将新评论更新
            questionService.updateById(question);
        }
        return Result.ok();
    }

    @Override
    public Result add(Replay replay) {
        //先将新增的回复插入到回复表
        boolean ok=this.save(replay);
        if(ok){
            //如果插入成功，取出对应的question,将question的回复数+1，再重新写会
            Question question=mapper.selectById(replay.getQuestionId());
            question.setReply(question.getReply()+1);
            mapper.updateById(question);
        }
        return Result.ok();
    }

    private void getIds(List<String> ids, String id) {
        QueryWrapper<Replay> wrapper=new QueryWrapper<>();
        wrapper.eq("parent_id",id);
        List<Replay> replays=baseMapper.selectList(wrapper);
        if(CollectionUtils.isNotEmpty(replays)){
            for(Replay replay:replays){
                //获取要删除的id
                String i=replay.getId();
                ids.add(i);
                //递归查找该id下的子评论
                this.getIds(ids,i);
            }
        }
    }
}
